package com.stericson.actionmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

public class ActionMenuActivity extends Activity {
	
	private MenuService menuService;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
	public MenuService getMenuService()
	{
		if (menuService == null)
			menuService = new MenuService(this);
		
		return menuService;
	}
	
    public void menu(View view)
    {
    	getMenuService().mainMenu();
    }
    
	public static boolean dismissMenu(MenuService menuService)
	{
		if (menuService.isVisible()){
			menuService.hideMenu();
			return true;
		}
		return false;
	}
	
	@Override
	public void onBackPressed()
	{
		if (!dismissMenu(getMenuService()))
		{
			finish();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_MENU) {
	    	if (this.findViewById(R.id.menuContainer) != null)
	    	{
	    		this.menu(this.findViewById(R.id.main));
	        	return true;
	    	}
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void startActivity(Intent intent)
	{
		getMenuService().hideMenu();
	    super.startActivity(intent);
	}
	
	@Override
	public void startActivityForResult(Intent intent, int requestCode)
	{
		getMenuService().hideMenu();
	    super.startActivityForResult(intent, requestCode);
	}
}